﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace Scan
{
    public class Helper
    {
        /// <summary>
        /// 对 ListView 的某列排序
        /// </summary>
        /// <param name="lstView"></param>
        /// <param name="e"></param>
        public static void SortListView(ListView lstView, ColumnClickEventArgs e)
        {
            ColHeader clickedCol = (ColHeader)lstView.Columns[e.Column];
            clickedCol.ascending = !clickedCol.ascending;
            int numItems = lstView.Items.Count;

            lstView.BeginUpdate();

            ArrayList sortArray = new ArrayList();
            for (int i = 0; i < numItems; i++)
            {
                sortArray.Add(new SortWrapper(lstView.Items[i], e.Column));
            }
            sortArray.Sort(0, sortArray.Count, new SortWrapper.SortComparer(clickedCol.ascending));

            lstView.Items.Clear();
            for (int i = 0; i < numItems; i++)
            {
                lstView.Items.Add(((SortWrapper)sortArray[i]).sortItem);
            }

            lstView.EndUpdate();
        }
    }
}
